<?php
/**
 * [ranknow bbs!] (C)2009-2015 ranknow.cn.
 * This is NOT a freeware, use is subject to license terms
 * $Id: _db.php 2009-12-29 liwei $
 */
function dbConnect() {
    global $cnn;
    $cnn = ( DB_PCONNECT ? mysql_pconnect( DB_HOST, DB_USERNAME, DB_PASSWORD ):
        mysql_connect( DB_HOST, DB_NAME, DB_PASSWORD ) ) or
    die( '数据库连接错误' );
    mysql_select_db( DB_NAME, $cnn ) or die( '数据库选择错误' );
    mysql_set_charset( 'utf8' );
}

if ( function_exists( 'mysql_set_charset' ) === false ) {
    function mysql_set_charset( $charset, $link_identifier = null ) {
        if ( $link_identifier == null ) {
            return mysql_query( 'SET CHARACTER SET "' . $charset . '"' );
        } else {
            return mysql_query( 'SET CHARACTER SET "' . $charset . '"', $link_identifier );
        }
    }
}

function &dbQuery( $sql ) {
    global $cnn;
    $rs = &mysql_query( $sql, $cnn );
    while ( $item = mysql_fetch_assoc( $rs ) ) {
        $data[] = $item;
    }
    return $data;
}

function &dbGetRow( $sql ) {
    global $cnn;
      $info = DEBUG?$sql:'sql语句执行错误';
    $rs = mysql_query( $sql ) or die( $info );
    if ( mysql_num_rows( $rs ) > 0 )
        return mysql_fetch_assoc( $rs );
    else
        return null;
}

function dbGetOne( $sql, $fildName ) {
    $rs = dbGetRow( $sql );
    return sizeof( $rs ) == null? null: ( isset( $rs[$fildName] )? $rs[$fildName]: null );
}

function &dbPageQuery( $sql, $page = 1, $pageSize = 20, $data = array() ) {
    if ( $page === null ) return dbQuery( $sql );
    $countSql = preg_replace( '|SELECT.*FROM|i', 'SELECT COUNT(*) count FROM', $sql );
    $n = ( int )dbGetOne( $countSql, 'count' );
    $data['pageSize'] = ( int )$pageSize < 1? 20: ( int )$pageSize;
    $data['recordCount'] = $n;

    $data['pageCount'] = ceil( $data['recordCount'] / $data['pageSize'] );
    $data['page'] = $data['pageCount'] == 0? 0: ( ( int )$page < 1? 1: ( int )$page );
    $data['page'] = $data['page'] > $data['pageCount']? $data['pageCount']:$data['page'];
    $data['isFirst'] = $data['page'] > 1? false: true;
    $data['isLast'] = $data['page'] < $data['pageCount']? false: true;
    $data['start'] = ( $data['page'] == 0 )? 0: ( $data['page']-1 ) * $data['pageSize'] + 1;
    $data['end'] = ( $data['start'] + $data['pageSize']-1 );
    $data['end'] = $data['end'] > $data['recordCount']? $data['recordCount']: $data['end'];

    $data['sql'] = $sql . ' LIMIT ' . ( ( $data['start'] > 0 )?$data['start']-1:$data['start'] ) . ',' . $data['pageSize'];
    // 传递分页所需要的其它参数
    foreach( $data as $key => $item ) {
        $data[$key] = $item;
    }
    $data['data'] = &dbQuery( $data['sql'] );
    return $data;
}

function dbExecute( $sql ) {
    global $cnn;
    mysql_query( $sql, $cnn ) or die( '' );
    return mysql_affected_rows( $cnn );
}

function dbDisconnect() {
    global $cnn;
    mysql_close( $cnn );
}

function sqlGetOneById( $table, $field, $id ) {
    return "SELECT * FROM $table WHERE $field=$id";
}

function sqlMakeInsert( $table, $data ) {
    $t1 = $t2 = array();
    foreach( $data as $key => $value ) {
        $t1[] = $key;
        $t2[] = "'" . addslashes( $value ) . "'";
    }
    return "INSERT INTO $table (" . implode( ",", $t1 ) . ") VALUES(" . implode( ",", $t2 ) . ")";
}

function sqlMakeReplace( $table, $data ) {
    $t1 = $t2 = array();
    foreach( $data as $key => $value ) {
        $t1[] = $key;
        $t2[] = "'" . addslashes( $value ) . "'";
    }
    return "REPLACE INTO $table (" . implode( ",", $t1 ) . ") VALUES(" . implode( ",", $t2 ) . ")";
}

function sqlMakeUpdateById( $table, $field, $id, $data ) {
    $t1 = array();
    foreach( $data as $key => $value ) {
        $t1[] = "$key='" . addslashes( $value ) . "'";
    }
    return "UPDATE $table SET " . implode( ",", $t1 ) . " WHERE $field=$id";
}

function sqlMakeDelById( $table, $field, $id ) {
    return "DELETE FROM $table WHERE $field=$id";
}

?>